import router from "@/router";
import storage from "./storage";
import store from "@/store";

router.beforeEach((to, from, next) => {
    let token = storage.get("token")
    //to 目标路由对象
    //from 源路由对象
    //next 是个函数，调用之后，可以理解为放行

    if (token) {
        // console.log('111',store)
        // console.log('222',store.state)
        
        if (!store.state.permission.userMenu.length) {
            store.dispatch('permission/FETCH_MENU_LIST').then(() => {
                console.log('to', to)
                next({ path: to.path, replace: true })
            }
            )
        } else {
            //已经登录，直接放行
            next()
        }
        // next()
    } else {
        //没有token
        //1访问的是登录页
        if (to.path === "/login") {
            next()
        } else {
            next({ path: "/login" })
        }
        //访问的不是登录页
    }

})
export default router